import Vue from 'vue'
import VueRouter from 'vue-router'
// 一级路由
import Layout from '@/views/layout'
import Detail from '@/views/detail'
import Login from '@/views/login'
import Register from '@/views/register'
// 二级路由
import BookList from '@/views/layout/BookList'
import SingleEditionList from '@/views/layout/SingleEditionList'
import ComparisonEditionList from '@/views/layout/ComparisonEditionList'
import ChineseAnnotationList from '@/views/layout/ChineseAnnotationList'
import ComparisonAnnotationList from '@/views/layout/ComparisonAnnotationList'

import BookInput from '@/views/detail/BookInput'
import BookEdit from '@/views/detail/BookEdit'
import SingleEditionInput from '@/views/detail/SingleEditionInput'
import ComparisonEditionInput from '@/views/detail/ComparisonEditionInput'
import ThreeDZEditionInput from '@/views/detail/ThreeDZEditionInput'
import ChineseAnnotationInput from '@/views/detail/ChineseAnnotationInput'
import ComparisonAnnotationInput from '@/views/detail/ComparisonAnnotationInput'
import imgsInput from '@/views/detail/imgsInput'

import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/register',
      component: Register
    },
    {
      path: '/',
      component: Layout,
      redirect: '/BookList',
      children: [
        {
          path: '/BookList',
          component: BookList
        },
        {
          path: '/SingleEditionList',
          component: SingleEditionList
        },
        {
          path: '/ComparisonEditionList',
          component: ComparisonEditionList
        },
        {
          path: '/ChineseAnnotationList',
          component: ChineseAnnotationList
        },
        {
          path: '/ComparisonAnnotationList',
          component: ComparisonAnnotationList
        }
      ]
    },
    {
      path: '/detail',
      component: Detail,
      children: [
        {
          // 新增书目
          path: '/BookInput',
          component: BookInput
        },
        {
          // 编辑书目
          path: '/BookEdit',
          component: BookEdit
        },
        {
          // 单语书目录入
          path: '/SingleEditionInput',
          component: SingleEditionInput
        },
        {
          // 双语对照录入
          path: '/ComparisonEditionInput',
          component: ComparisonEditionInput
        },
        {
          // 三语对照录入
          path: '/ThreeDZEditionInput',
          component: ThreeDZEditionInput
        },
        {
          path: '/ChineseAnnotationInput',
          component: ChineseAnnotationInput
        },
        {
          path: '/ComparisonAnnotationInput',
          component: ComparisonAnnotationInput
        },
        {
          // 图片上传页面
          path: '/imgsInput',
          component: imgsInput
        }
      ]
    }
  ]
})
// 用一个数组装不需要验证的网址
const noAuthUrl = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = store.getters.token
  // 如果没有在数组里出现过，那就需要验证token
  if (!noAuthUrl.includes(to.path)) {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
